var CONVERT_ARRAY = ['间', '接', '接领', '接', '接领', '赚', '赚', '钱', '补', '金', '倍', '接', '接', '视频', '览', '看', '频', '额', '领', '容', '内', '内容', '停', '键', '领', '领', '提',
    '情', '索', '成', '停', '短', '毛', '享', '一', '账', '索赚', '视频', '赚', '羊', '取', '赚金', '短剧赚', '表', '贴', '接领', '开启', '最', '视频最', '领', '一', '键', '并', '+', '择', '要',
    '现', '免', '费', '免费', '饭', '赢', '弃', '挑', '视', '清', '知', '赢', '成', '取', '千', '签', '已', '视', '看', '看视', '看视', '看视', '看视', '视', '千', '详', '户',
    '情', '情', '换', '预', '高', '约', '关', '清', '0', '看', '明', '没', '赚', '频', '70', '接', '赚', '得', '币', '预', '约', '预约', '装', '(', '意', '载', '额', '客', '币',
    '情', '觉']
var REPLACE_ARRAY = ['问', '投', '投顿', '按', '按顿', '賺', '嫌', '线', '朴', '全', '信', '援', '技', '规频', '覧', '春', '頻', '額', '顿', '客', '內', '內客', '傳', '鍵', '領', '颌', '堤',
    '憤', '素', '戌', '惇', '矩', 'モ', '亨', 'ー', '败', '素賺', '视', '|赚', '|羊', '|取', '賺全', '矩剧賺', '衷', '见贴', '技颌', '启', '景', '规频景', '领颌', '-', '建', '井', '十', '擇', '雯',
    '現', '兔', '費', '兔費', '源', '嘉', '奔', '找', '視', '请', '如', '贏', '戍', '职', '于', '笠', '己', '机', '着', '着规', '春规', '着視', '春視', '初', '干', '洋', '戶',
    '婧', '倩', '換', '频', '富', '約', '美', '潰', 'O', '者', '明', '设', '赚兼', '颊', '/0', '授', '兼', '倡', '市', '預', '約', '預約', '裝', ' (', '薏', '載', '颜', '害', '市',
    '惰', '党']



// 训练 -- 领取, 看视频, 看广告

function requestScreen权限() {
    if (!permissionManger.checkScreenShotPremission()) {
        permissionManger.requesScreenShotPremission()

        sleep.millisecond(300)
        while (!permissionManger.checkScreenShotPremission()) {
            if (hid.isOn()) {
                // if (true) {
                Otg.click(screen.percentToWidth(0.75), screen.percentToHeight(1) - 100)
                sleep.millisecond(200)
                Otg.click(screen.percentToWidth(0.75), screen.percentToHeight(0.6))
                sleep.millisecond(200)
                Otg.click(screen.percentToWidth(0.75), screen.percentToHeight(0.93))
            } else {
                agent.click(screen.percentToWidth(0.75), screen.percentToHeight(1) - 100)
                sleep.millisecond(200)
                agent.click(screen.percentToWidth(0.75), screen.percentToHeight(0.6))
                sleep.millisecond(200)
                agent.click(screen.percentToWidth(0.75), screen.percentToHeight(0.93))
            }
            sleep.millisecond(500)
        }
        agent.home()
    }
}

function printocrinfo(msg) {
    SystemPrint.log(msg)
}

function replaceFind(ocrRes, text, index) {
    if (index >= CONVERT_ARRAY.length) {
        return null
    }

    // printocrinfo(text.replace(CONVERT_ARRAY[index], REPLACE_ARRAY[index]))
    var dect = ocrRes.findIncludeText(text.replace(CONVERT_ARRAY[index], REPLACE_ARRAY[index]));
    if (!dect) {
        return replaceFind(ocrRes, text, index + 1)
    }
    return dect
}


function replaceFindAll(ocrRes, text, index) {
    if (index >= CONVERT_ARRAY.length) {
        return null
    }

    // printocrinfo(text.replace(CONVERT_ARRAY[index], REPLACE_ARRAY[index]))
    var dects = ocrRes.findIncludeTexts(text.replace(CONVERT_ARRAY[index], REPLACE_ARRAY[index]));
    if (!dects || dects.length == 0) {
        return replaceFindAll(ocrRes, text, index + 1)
    }
    return dects
}


function replaceFindComplate(ocrRes, text, index) {
    if (index >= CONVERT_ARRAY.length) {
        return null
    }

    // printocrinfo(text.replace(CONVERT_ARRAY[index], REPLACE_ARRAY[index]))
    var dect = ocrRes.findText(text.replace(CONVERT_ARRAY[index], REPLACE_ARRAY[index]));
    if (!dect) {
        return replaceFindComplate(ocrRes, text, index + 1)
    }
    return dect
}


function ocrTextTrue(text) {
    if (!text) {
        printocrinfo('未定义text')
        return null
    }
    var img = screen.screenShotFull();
    if (img.isNull()) {
        // requestScreen权限()
        return null
    }
    var ocrRes = img.MLKitOcr('zhs');
    printocrinfo(ocrRes)
    img.recycle()

    //结果中找字
    var dect = ocrRes.findIncludeText(text);

    //       var img = screen.screenShotFull();
    //     var ocrRes = img.MLKitOcr('zhs');
    //      var dect = ocrRes.findIncludeText('短剧');
    //     //  var alldercts = ocrRes.getAllDetect()
    //     printl( dect)
    //    printl(ocrRes)

    if (!dect && CONVERT_ARRAY.some(str => text.includes(str))) {
        dect = replaceFind(ocrRes, text, 0)
    }
    if (dect) printocrinfo(dect)
    return dect
}

function ocrGetAllStr() {
    var img = screen.screenShotFull();
    if (img.isNull()) {
        // requestScreen权限()
        return null
    }

    var ocrRes = img.MLKitOcrRect('zhs', [screen.widthToPercent(20), 0, 1, 1]);
    img.recycle()
    return ocrRes.getAllString()
}

function ocrGetAllStrsByText(text) {
    var img = screen.screenShotFull();
    if (img.isNull()) {
        // requestScreen权限()
        return null
    }

    var ocrRes = img.MLKitOcrRect('zhs', [screen.widthToPercent(20), 0, 1, 1]);
    img.recycle()

    var dects = ocrRes.findIncludeTexts(text);
    if ((!dects || dects.length == 0) && CONVERT_ARRAY.some(str => text.includes(str))) {
        dects = replaceFindAll(ocrRes, text, 0)
    }

    for (var i = 0; i < dects.length; i++) {
        SystemPrint.log(dects[i])
    }

    return dects
}

function ocrGetAllStrsBy相似的Text(text) {
    var img = screen.screenShotFull();
    if (img.isNull()) {
        // requestScreen权限()
        return null
    }

    var ocrRes = img.MLKitOcrRect('zhs', [screen.widthToPercent(20), 0, 1, 1]);
    img.recycle()

    var dects = ocrRes.findIncludeTexts(text);
    if (CONVERT_ARRAY.some(str => text.includes(str))) {
        var newDects = replaceFindAll(ocrRes, text, 0)
        SystemPrint.log(newDects.length)
        if (dects && dects.length > 0) {
            for (let i = 0; i < newDects.length - 1; i++) {
                dects.push(newDects[i])
            }

        } else {
            dects = newDects
        }

    }

    for (var i = 0; i < dects.length; i++) {
        SystemPrint.log(dects[i])
    }

    return dects
}

function ocrGetAllStrInRegion(percentX1, percentY1, percentX2, percentY2) {
    var img = screen.screenShotFull();
    if (img.isNull()) {
        // requestScreen权限()
        return null
    }

    var ocrRes = img.MLKitOcrRect('zhs', [percentX1, percentY1, percentX2, percentY2]);
    img.recycle()
    return ocrRes.getAllString().replaceAll('T', '1')
}

function ocrGetAllStrInNearDect(dect, offectTop, offectBottom) {
    if (!dect) {
        return ''
    }
    var findRect = dect.getRealRect()

    var img = screen.screenShotFull();
    if (img.isNull()) {
        // requestScreen权限()
        return null
    }

    var ocrRes = img.MLKitOcrRect('zhs', [screen.widthToPercent(20), screen.heightToPercent(findRect.getTop() - offectTop), 1, screen.heightToPercent(findRect.getBottom() + offectBottom)]);
    img.recycle()
    SystemPrint.log(ocrRes.getAllString())
    return ocrRes.getAllString().replaceAll('T', '1')
}

function ocrTextInRegion(percentX1, percentY1, percentX2, percentY2, text) {
    if (percentX1 < 0 || percentY1 < 0 || percentX2 < 0 || percentY2 < 0) return null
    var img = screen.screenShotFull();
    if (img.isNull()) {
        // requestScreen权限()
        print.err('截图失败!!!')
        return null
    }
    // var cutimg = img.cutImagePercent(percentX1, percentY1, percentX2, percentY2)
    // var ocrRes = cutimg.MLKitOcr('zhs');
    var ocrRes = img.MLKitOcrRect('zhs', [percentX1, percentY1, percentX2, percentY2]);
    // printocrinfo(ocrRes)
    img.recycle()
    // cutimg.recycle()

    //结果中找字
    var dect = ocrRes.findIncludeText(text);
    if (!dect && CONVERT_ARRAY.some(str => text.includes(str))) {
        dect = replaceFind(ocrRes, text, 0)
    }
    if (dect) {
        // var rect = dect.getRealRect()

        // rect.set(rect.getLeft() + screen.percentToWidth(percentX1), rect.getTop() + screen.percentToHeight(percentY1), rect.getRight() + screen.percentToWidth(percentX1), rect.getBottom() + screen.percentToHeight(percentY1))
        printocrinfo(dect)
    }


    return dect
}

function ocrText(text, text2, text3, text4, text5, text6, text7) {
    if (!text) {
        return null
    }

    var current = new Date();
    var hours = current.getHours().toString().padStart(2, '0');
    var minutes = current.getMinutes().toString().padStart(2, '0');

    sleep.millisecond(200)
    var img = screen.screenShotFull();
    if (!img || img.isNull()) {
        return null
    }
    txt.writeTxt('/sdcard/aiworklog.txt', hours + ':' + minutes + '--' + 'ocrText -> ' + text + '\n', true)

   var ocrRes = img.MLKitOcrRect('zhs', [screen.widthToPercent(20), 0, 1, 1]);
    // printocrinfo(ocrRes)
    if (img != null) {
        img.recycle()
    }

    var dect = ocrRes.findIncludeText(text)
    if (!dect && CONVERT_ARRAY.some(str => text.includes(str))) {
        dect = replaceFind(ocrRes, text, 0)
    }

    // dect.getRealRect().getCenterY

    if (!dect && text2 != undefined && typeof (text2) == 'string') {
        dect = ocrRes.findIncludeText(text2)
        if (!dect && CONVERT_ARRAY.some(str => text2.includes(str))) {
            dect = replaceFind(ocrRes, text2, 0)
        }
    }

    if (!dect && text3 != undefined && typeof (text3) == 'string') {
        dect = ocrRes.findIncludeText(text3)
        if (!dect && CONVERT_ARRAY.some(str => text3.includes(str))) {
            dect = replaceFind(ocrRes, text3, 0)
        }
    }

    if (!dect && text4 != undefined && typeof (text4) == 'string') {
        dect = ocrRes.findIncludeText(text4)
        if (!dect && CONVERT_ARRAY.some(str => text4.includes(str))) {
            dect = replaceFind(ocrRes, text4, 0)
        }
    }

    if (!dect && text5 != undefined && typeof (text5) == 'string') {
        dect = ocrRes.findIncludeText(text5)
        if (!dect && CONVERT_ARRAY.some(str => text5.includes(str))) {
            dect = replaceFind(ocrRes, text5, 0)
        }
    }

    if (!dect && text6 != undefined && typeof (text6) == 'string') {
        dect = ocrRes.findIncludeText(text6)
        if (!dect && CONVERT_ARRAY.some(str => text6.includes(str))) {
            dect = replaceFind(ocrRes, text6, 0)
        }
    }

    if (!dect && text7 != undefined && typeof (text7) == 'string') {
        dect = ocrRes.findIncludeText(text7)
        if (!dect && CONVERT_ARRAY.some(str => text7.includes(str))) {
            dect = replaceFind(ocrRes, text7, 0)
        }
    }

    if (dect) {
        txt.writeTxt('/sdcard/aiworklog.txt', hours + ':' + minutes + '--' + text + '\n', true)
        printocrinfo(dect)
    }

    return dect
}

function ocrAllRes() {
    var img = screen.screenShotFull();
    if (img.isNull()) {
        // requestScreen权限()
        return null
    }
    var ocrRes = img.MLKitOcrRect('zhs', [screen.widthToPercent(20), 0, 1, 1]);
    // printocrinfo(ocrRes)
    img.recycle()

    return ocrRes
}

function ocrTextMoreByRes(ocrRes, text, text2, text3, text4, text5, text6, text7, text8, text9) {
    if (!ocrRes) {
        return null
    }

    var dect = ocrRes.findIncludeText(text)
    if (!dect && CONVERT_ARRAY.some(str => text.includes(str))) {
        dect = replaceFind(ocrRes, text, 0)
    }

    // dect.getRealRect().getCenterY

    if (!dect && text2 != undefined && typeof (text2) == 'string') {
        dect = ocrRes.findIncludeText(text2)
        if (!dect && CONVERT_ARRAY.some(str => text2.includes(str))) {
            dect = replaceFind(ocrRes, text2, 0)
        }
    }

    if (!dect && text3 != undefined && typeof (text3) == 'string') {
        dect = ocrRes.findIncludeText(text3)
        if (!dect && CONVERT_ARRAY.some(str => text3.includes(str))) {
            dect = replaceFind(ocrRes, text3, 0)
        }
    }

    if (!dect && text4 != undefined && typeof (text4) == 'string') {
        dect = ocrRes.findIncludeText(text4)
        if (!dect && CONVERT_ARRAY.some(str => text4.includes(str))) {
            dect = replaceFind(ocrRes, text4, 0)
        }
    }

    if (!dect && text5 != undefined && typeof (text5) == 'string') {
        dect = ocrRes.findIncludeText(text5)
        if (!dect && CONVERT_ARRAY.some(str => text5.includes(str))) {
            dect = replaceFind(ocrRes, text5, 0)
        }
    }

    if (!dect && text6 != undefined && typeof (text6) == 'string') {
        dect = ocrRes.findIncludeText(text6)
        if (!dect && CONVERT_ARRAY.some(str => text6.includes(str))) {
            dect = replaceFind(ocrRes, text6, 0)
        }
    }

    if (!dect && text7 != undefined && typeof (text7) == 'string') {
        dect = ocrRes.findIncludeText(text7)
        if (!dect && CONVERT_ARRAY.some(str => text7.includes(str))) {
            dect = replaceFind(ocrRes, text7, 0)
        }
    }

    if (!dect && text8 != undefined && typeof (text8) == 'string') {
        dect = ocrRes.findIncludeText(text8)
        if (!dect && CONVERT_ARRAY.some(str => text8.includes(str))) {
            dect = replaceFind(ocrRes, text8, 0)
        }
    }

    if (!dect && text9 != undefined && typeof (text9) == 'string') {
        dect = ocrRes.findIncludeText(text9)
        if (!dect && CONVERT_ARRAY.some(str => text9.includes(str))) {
            dect = replaceFind(ocrRes, text9, 0)
        }
    }


    if (dect) printocrinfo(dect)
    return dect
}

function ocrTextMoreCompleteByRes(ocrRes, text, text2, text3, text4, text5, text6) {
    if (!ocrRes) {
        return null
    }

    var dect = ocrRes.findText(text)
    if (!dect && CONVERT_ARRAY.some(str => text.includes(str))) {
        dect = replaceFindComplate(ocrRes, text, 0)
    }
    // dect.getRealRect().getCenterY

    if (!dect && text2 != undefined) {
        dect = ocrRes.findText(text2)
        if (!dect && CONVERT_ARRAY.some(str => text2.includes(str))) {
            dect = replaceFindComplate(ocrRes, text2, 0)
        }
    }

    if (!dect && text3 != undefined) {
        dect = ocrRes.findText(text3)
        if (!dect && CONVERT_ARRAY.some(str => text3.includes(str))) {
            dect = replaceFindComplate(ocrRes, text3, 0)
        }
    }

    if (!dect && text4 != undefined) {
        dect = ocrRes.findText(text4)
        if (!dect && CONVERT_ARRAY.some(str => text4.includes(str))) {
            dect = replaceFindComplate(ocrRes, text4, 0)
        }
    }

    if (!dect && text5 != undefined) {
        dect = ocrRes.findText(text5)
        if (!dect && CONVERT_ARRAY.some(str => text5.includes(str))) {
            dect = replaceFindComplate(ocrRes, text5, 0)
        }
    }

    if (!dect && text6 != undefined) {
        dect = ocrRes.findText(text6)
        if (!dect && CONVERT_ARRAY.some(str => text6.includes(str))) {
            dect = replaceFindComplate(ocrRes, text6, 0)
        }
    }
    if (dect) printocrinfo(dect)
    return dect
}

function ocrTextMoreComplete(text, text2, text3, text4, text5, text6) {
    if (!text) {
        return null
    }
    var img = screen.screenShotFull();
    if (img.isNull()) {
        // requestScreen权限()
        return null
    }
    var ocrRes = img.MLKitOcrRect('zhs', [screen.widthToPercent(20), 0, 1, 1]);
    // printocrinfo(ocrRes)
    img.recycle()


    var dect = ocrRes.findText(text)
    if (!dect && CONVERT_ARRAY.some(str => text.includes(str))) {
        dect = replaceFindComplate(ocrRes, text, 0)
    }
    // dect.getRealRect().getCenterY

    if (!dect && text2 != undefined) {
        dect = ocrRes.findText(text2)
        if (!dect && CONVERT_ARRAY.some(str => text2.includes(str))) {
            dect = replaceFindComplate(ocrRes, text2, 0)
        }
    }

    if (!dect && text3 != undefined) {
        dect = ocrRes.findText(text3)
        if (!dect && CONVERT_ARRAY.some(str => text3.includes(str))) {
            dect = replaceFindComplate(ocrRes, text3, 0)
        }
    }

    if (!dect && text4 != undefined) {
        dect = ocrRes.findText(text4)
        if (!dect && CONVERT_ARRAY.some(str => text4.includes(str))) {
            dect = replaceFindComplate(ocrRes, text4, 0)
        }
    }

    if (!dect && text5 != undefined) {
        dect = ocrRes.findText(text5)
        if (!dect && CONVERT_ARRAY.some(str => text5.includes(str))) {
            dect = replaceFindComplate(ocrRes, text5, 0)
        }
    }

    if (!dect && text6 != undefined) {
        dect = ocrRes.findText(text6)
        if (!dect && CONVERT_ARRAY.some(str => text6.includes(str))) {
            dect = replaceFindComplate(ocrRes, text6, 0)
        }
    }
    if (dect) printocrinfo(dect)
    return dect
}

function ocrTextMoreInCenter(text, text2, text3, text4, text5, text6, text7) {
    // printocrinfo('testtt')
    var img = screen.screenShotFull();
    if (img.isNull()) {
        // requestScreen权限()
        return null
    }
    // var cutimg = img.cutImagePercent(percentX1, percentY1, percentX2, percentY2)
    // var ocrRes = cutimg.MLKitOcr('zhs');
    var ocrRes = img.MLKitOcrRect('zhs', [screen.widthToPercent(20), 0.3, 1, 0.85]);


    img.recycle()
    // console.timeEnd()
    // cutimg.recycle()
    // printocrinfo(ocrRes)
    var dect = ocrRes.findIncludeText(text)
    // printocrinfo(text)
    // printocrinfo(dect)
    if (!dect && CONVERT_ARRAY.some(str => text.includes(str))) {
        dect = replaceFind(ocrRes, text, 0)
    }
    // printocrinfo(text2)
    if (!dect && text2 != undefined) {
        dect = ocrRes.findIncludeText(text2)
        if (!dect && CONVERT_ARRAY.some(str => text2.includes(str))) {
            dect = replaceFind(ocrRes, text2, 0)
        }
    }

    if (!dect && text3 != undefined) {
        dect = ocrRes.findIncludeText(text3)
        if (!dect && CONVERT_ARRAY.some(str => text3.includes(str))) {
            dect = replaceFind(ocrRes, text3, 0)
        }
    }

    if (!dect && text4 != undefined) {
        dect = ocrRes.findIncludeText(text4)
        if (!dect && CONVERT_ARRAY.some(str => text4.includes(str))) {
            dect = replaceFind(ocrRes, text4, 0)
        }
    }

    if (!dect && text5 != undefined) {
        dect = ocrRes.findIncludeText(text5)
        if (!dect && CONVERT_ARRAY.some(str => text5.includes(str))) {
            dect = replaceFind(ocrRes, text5, 0)
        }
    }

    if (!dect && text6 != undefined) {
        dect = ocrRes.findIncludeText(text6)
        if (!dect && CONVERT_ARRAY.some(str => text6.includes(str))) {
            dect = replaceFind(ocrRes, text6, 0)
        }
    }

    if (!dect && text7 != undefined) {
        dect = ocrRes.findIncludeText(text7)
        if (!dect && CONVERT_ARRAY.some(str => text7.includes(str))) {
            dect = replaceFind(ocrRes, text7, 0)
        }
    }


    if (dect) printocrinfo(dect)
    return dect
}

function ocrTextMoreInRegion(percentX1, percentY1, percentX2, percentY2, text, text2, text3, text4, text5, text6, text7) {
    if (percentX1 < 0 || percentY1 < 0 || percentX2 < 0 || percentY2 < 0) return null

    // printocrinfo('testtt')
    var img = screen.screenShotFull();
    if (img.isNull()) {
        // requestScreen权限()
        return null
    }
    // var cutimg = img.cutImagePercent(percentX1, percentY1, percentX2, percentY2)
    // var ocrRes = cutimg.MLKitOcr('zhs');
    var ocrRes = img.MLKitOcrRect('zhs', [percentX1, percentY1, percentX2, percentY2]);


    img.recycle()
    // console.timeEnd()
    // cutimg.recycle()
    // printocrinfo(ocrRes)
    var dect = ocrRes.findIncludeText(text)
    // printocrinfo(text)
    printocrinfo(dect)
    if (!dect && CONVERT_ARRAY.some(str => text.includes(str))) {
        dect = replaceFind(ocrRes, text, 0)
    }
    // printocrinfo(text2)
    if (!dect && text2 != undefined) {
        dect = ocrRes.findIncludeText(text2)
        if (!dect && CONVERT_ARRAY.some(str => text2.includes(str))) {
            dect = replaceFind(ocrRes, text2, 0)
        }
    }

    if (!dect && text3 != undefined) {
        dect = ocrRes.findIncludeText(text3)
        if (!dect && CONVERT_ARRAY.some(str => text3.includes(str))) {
            dect = replaceFind(ocrRes, text3, 0)
        }
    }

    if (!dect && text4 != undefined) {
        dect = ocrRes.findIncludeText(text4)
        if (!dect && CONVERT_ARRAY.some(str => text4.includes(str))) {
            dect = replaceFind(ocrRes, text4, 0)
        }
    }

    if (!dect && text5 != undefined) {
        dect = ocrRes.findIncludeText(text5)
        if (!dect && CONVERT_ARRAY.some(str => text5.includes(str))) {
            dect = replaceFind(ocrRes, text5, 0)
        }
    }

    if (!dect && text6 != undefined) {
        dect = ocrRes.findIncludeText(text6)
        if (!dect && CONVERT_ARRAY.some(str => text6.includes(str))) {
            dect = replaceFind(ocrRes, text6, 0)
        }
    }

    if (!dect && text7 != undefined) {
        dect = ocrRes.findIncludeText(text7)
        if (!dect && CONVERT_ARRAY.some(str => text7.includes(str))) {
            dect = replaceFind(ocrRes, text7, 0)
        }
    }


    if (dect) printocrinfo(dect)
    return dect
}

function ocrTextMoreInRegionComplete(percentX1, percentY1, percentX2, percentY2, text, text2, text3, text4, text5, text6) {
    var img = screen.screenShotFull();
    if (img.isNull()) {
        // requestScreen权限()
        return null
    }
    // var cutimg = img.cutImagePercent(percentX1, percentY1, percentX2, percentY2)
    // var ocrRes = cutimg.MLKitOcr('zhs');
    var ocrRes = img.MLKitOcrRect('zhs', [percentX1, percentY1, percentX2, percentY2]);
    // printocrinfo(ocrRes)
    img.recycle()
    // cutimg.recycle()

    var dect = ocrRes.findText(text)
    if (!dect && CONVERT_ARRAY.some(str => text.includes(str))) {
        dect = replaceFindComplate(ocrRes, text, 0)
    }
    if (!dect && text2 != undefined) {
        dect = ocrRes.findText(text2)
        if (!dect && CONVERT_ARRAY.some(str => text2.includes(str))) {
            dect = replaceFindComplate(ocrRes, text2, 0)
        }
    }

    if (!dect && text3 != undefined) {
        dect = ocrRes.findText(text3)
        if (!dect && CONVERT_ARRAY.some(str => text3.includes(str))) {
            dect = replaceFindComplate(ocrRes, text3, 0)
        }
    }

    if (!dect && text4 != undefined) {
        dect = ocrRes.findText(text4)
        if (!dect && CONVERT_ARRAY.some(str => text4.includes(str))) {
            dect = replaceFindComplate(ocrRes, text4, 0)
        }
    }

    if (!dect && text5 != undefined) {
        dect = ocrRes.findText(text5)
        if (!dect && CONVERT_ARRAY.some(str => text5.includes(str))) {
            dect = replaceFindComplate(ocrRes, text5, 0)
        }
    }


    if (dect) {
        // var rect = dect.getRealRect()
        // rect.set(rect.getLeft() + screen.percentToWidth(percentX1), rect.getTop() + screen.percentToHeight(percentY1), rect.getRight() + screen.percentToWidth(percentX1), rect.getBottom() + screen.percentToHeight(percentY1))
        printocrinfo(dect)
    }

    return dect
}

/**
 * @param text ...
 * @param times 执行次数 默认3
 * @param stepSecond 每次执行的间隔多少秒, 默认2s
 */
function ocrWaitForText(text, text2, text3, text4, text5, text6, text7, times, stepSecond) {
    if (typeof (times) == 'string') {
        SystemPrint.err('ocrWaitForMoreTextMillisecond' + times)
        times = 3
    }
    // ocrWaitForText('点击领取', '退出', 18, 1)
    var current = new Date();
    var hours = current.getHours().toString().padStart(2, '0');
    var minutes = current.getMinutes().toString().padStart(2, '0');

    txt.writeTxt('/sdcard/aiworklog.txt', hours + ':' + minutes + '--' + text + '\n', true)

    if (stepSecond != undefined && (typeof (stepSecond) == 'string') || stepSecond > 50) {
        stepSecond = 2
    }
    var dect = null

    if (times == undefined) times = 3
    if (stepSecond == undefined) stepSecond = 2
    var whileTimes = 0
    var haspd = false

    while (dect == null) {
        console.time()
        if (whileTimes >= times) {
            break
        }
        whileTimes = whileTimes + 1

        if (text2 == undefined) {
            dect = ocrText(text);
        }
        else if (typeof (text2) == 'number') {
            dect = ocrText(text);
            times = text2
            if (typeof (text3) == 'number') {
                stepSecond = text3
            }
        }
        else if (typeof (text3) == 'number') {
            dect = ocrText(text, text2);
            times = text3
            if (typeof (text4) == 'number') {
                stepSecond = text4
            }
        }
        else if (typeof (text4) == 'number') {
            dect = ocrText(text, text2, text3);
            times = text4
            if (typeof (text5) == 'number') {
                stepSecond = text5
            }
        }
        else if (typeof (text5) == 'number') {
            dect = ocrText(text, text2, text3, text4);
            times = text5
            if (typeof (text6) == 'number') {
                stepSecond = text6
            }
        }
        else if (typeof (text6) == 'number') {
            dect = ocrText(text, text2, text3, text4, text5);
            times = text6
            if (typeof (text7) == 'number') {
                stepSecond = text7
            }
        }
        else if (typeof (text7) == 'number') {
            dect = ocrText(text, text2, text3, text4, text5, text6);
            if (haspd == false && times != undefined && typeof (times) == 'number') {
                stepSecond = times
                haspd = true
            }
            times = text7
        }
        else if (typeof (times) == 'number') {
            dect = ocrText(text, text2, text3, text4, text5, text6, text7);
        } else {
            dect = ocrText(text, text2, text3, text4, text5, text6, text7);
        }

        // SystemPrint.log(stepSecond)

        var differTime = stepSecond * 1000 - console.timeEnd()
        if (!dect && (differTime > 10)) {
            sleep.millisecond(differTime)
        }
    }
    // if (dect) printocrinfo(dect)
    return dect
}

// function ocrWaitForText(text, times, second) {
//     if (!text) {
//         return
//     }
//     if (times == undefined) times = 2
//     if (second == undefined) second = 1

//     var img = screen.screenShotFull();
//     if (img.isNull()) {
//         // requestScreen权限()
//         return null
//     }
//     var ocrRes = img.MLKitOcr('zhs');
//     printocrinfo(ocrRes)

//     var dect = ocrRes.findIncludeText(text);
//     if (!dect && CONVERT_ARRAY.some(str => text.includes(str))) {
//         dect = replaceFind(ocrRes, text, 0)
//     }
//     if (!dect) {
//         sleep.second(second)
//     }
//     var whileTimes = 0
//     while (dect == null) {
//         if (whileTimes > times) {
//             break
//         }
//         whileTimes = whileTimes + 1
//         img = screen.screenShotFull();
//         if (img.isNull()) {
//             break
//         }
//         ocrRes = img.MLKitOcr('zhs');
//         dect = ocrRes.findIncludeText(text);
//         if (!dect && CONVERT_ARRAY.some(str => text.includes(str))) {
//             dect = replaceFind(ocrRes, text, 0)
//         }
//         var differTime = second * 1000 - console.timeEnd()
//         if (!dect && differTime > 10) {
//             sleep.millisecond(differTime)
//         }
//     }
//     img.recycle()

//     if (dect) printocrinfo(dect)

//     return dect
// }


function ocrWaitForTextMillisecondInRegion(percentX1, percentY1, percentX2, percentY2, text, times, millisecond) {
    if (times == undefined) times = 3
    if (millisecond == undefined) millisecond = 500
    var img = screen.screenShotFull();
    if (img.isNull()) {
        // requestScreen权限()
        return null
    }
    // var cutimg = img.cutImagePercent(percentX1, percentY1, percentX2, percentY2)
    // var ocrRes = cutimg.MLKitOcr('zhs');
    var ocrRes = img.MLKitOcrRect('zhs', [percentX1, percentY1, percentX2, percentY2]);

    var dect = ocrRes.findIncludeText(text);
    if (!dect && CONVERT_ARRAY.some(str => text.includes(str))) {
        dect = replaceFind(ocrRes, text, 0)
    }
    if (!dect) {
        sleep.millisecond(millisecond)
    }
    var whileTimes = 0
    while (dect == null) {
        console.time()
        if (whileTimes >= times) {
            break
        }
        whileTimes = whileTimes + 1
        img = screen.screenShotFull();
        if (img.isNull()) {
            break
        }
        // cutimg = img.cutImagePercent(percentX1, percentY1, percentX2, percentY2)
        // ocrRes = cutimg.MLKitOcr('zhs');
        ocrRes = img.MLKitOcrRect('zhs', [percentX1, percentY1, percentX2, percentY2]);

        dect = ocrRes.findIncludeText(text);
        if (!dect && CONVERT_ARRAY.some(str => text.includes(str))) {
            dect = replaceFind(ocrRes, text, 0)
        }
        var differTime = millisecond - console.timeEnd()
        if (!dect && (differTime > 10)) {
            sleep.millisecond(differTime)
        }
    }
    img.recycle()
    // cutimg.recycle()

    if (dect) {
        // var rect = dect.getRealRect()
        // rect.set(rect.getLeft() + screen.percentToWidth(percentX1), rect.getTop() + screen.percentToHeight(percentY1), rect.getRight() + screen.percentToWidth(percentX1), rect.getBottom() + screen.percentToHeight(percentY1))
        printocrinfo(dect)
    }

    return dect
}

/**
 * 每次执行的间隔多少秒, 默认500毫秒
 * @param text ...
 * @param times 执行次数 默认6
 * @param millisecond 执行次数 默认500
 */
function ocrWaitForMoreTextMillisecond(text, text2, text3, text4, text5, text6, text7, times, millisecond) {
    if (times != undefined && typeof (times) == 'string') {
        SystemPrint.err('ocrWaitForMoreTextMillisecond' + times)
        times = 6
    }
    if (millisecond != undefined && millisecond < 100) {
        millisecond = 500
    }
    if (times == undefined) times = 6
    if (millisecond == undefined) millisecond = 500

    var dect = null
    var whileTimes = 0
    var haspd = false
    while (dect == null) {
        console.time()
        if (whileTimes >= times) {
            break
        }
        whileTimes = whileTimes + 1

        if (text2 == undefined) {
            dect = ocrText(text);
        }
        else if (typeof (text2) == 'number') {
            dect = ocrText(text);
            times = text2
            if (typeof (text3) == 'number') {
                millisecond = text3
            }
        }
        else if (typeof (text3) == 'number') {
            dect = ocrText(text, text2);
            times = text3
            if (typeof (text4) == 'number') {
                millisecond = text4
            }
        }
        else if (typeof (text4) == 'number') {
            dect = ocrText(text, text2, text3);
            times = text4
            if (typeof (text5) == 'number') {
                millisecond = text5
            }
        }
        else if (typeof (text5) == 'number') {
            dect = ocrText(text, text2, text3, text4);
            times = text5
            if (typeof (text6) == 'number') {
                millisecond = text6
            }
        }
        else if (typeof (text6) == 'number') {
            dect = ocrText(text, text2, text3, text4, text5);
            times = text6
            if (typeof (text7) == 'number') {
                millisecond = text7
            }
        }
        else if (typeof (text7) == 'number') {
            dect = ocrText(text, text2, text3, text4, text5, text6);
            if (haspd == false && times != undefined && typeof (times) == 'number') {
                millisecond = times
                haspd = true
            }
            times = text7
        }
        else if (typeof (times) == 'number') {
            dect = ocrText(text, text2, text3, text4, text5, text6, text7);
        } else {
            dect = ocrText(text, text2, text3, text4, text5, text6, text7);
        }
        var differTime = millisecond - console.timeEnd()
        if (!dect && (differTime > 100)) {
            sleep.millisecond(differTime)
        }
    }
    if (dect) printocrinfo(dect)
    return dect
}

function ocrWaitForMoreTextMillisecondInRegion(percentX1, percentY1, percentX2, percentY2, times, text1, text2, text3, text4, text5, text6, text7) {
    if (times == undefined) times = 3
    var millisecond = 500;

    var dect = ocrTextMoreInRegion(percentX1, percentY1, percentX2, percentY2, text1, text2, text3, text4, text5, text6, text7);
    printocrinfo(dect)
    if (!dect) {
        sleep.millisecond(millisecond)
    }
    var whileTimes = 0
    while (dect == null) {
        console.time()
        if (whileTimes >= times) {
            break
        }
        whileTimes = whileTimes + 1
        dect = ocrTextMoreInRegion(percentX1, percentY1, percentX2, percentY2, text1, text2, text3, text4, text5, text6, text7);
        var differTime = millisecond - console.timeEnd()
        if (!dect && (differTime > 10)) {
            sleep.millisecond(differTime)
        }
    }

    // if (dect) {
    //     var rect = dect.getRealRect()
    //     rect.set(rect.getLeft() + screen.percentToWidth(percentX1), rect.getTop() + screen.percentToHeight(percentY1), rect.getRight() + screen.percentToWidth(percentX1), rect.getBottom() + screen.percentToHeight(percentY1))
    //     printocrinfo(dect)
    // }
    return dect
}


/**
 * 是否在目标附近
 * @param findView 
 * @param offectY 上下偏移量, 默认100
 */
function isInNearBy(findView, text, text2, text3, offectY) {
    if (!findView) {
        return false
    }
    if (offectY == undefined) offectY = 100

    var findRect = findView.getRealRect()

    if (text2 == undefined) {
        if (ocrTextMoreInRegion(0, screen.heightToPercent(findRect.getTop() - offectY), 1, screen.heightToPercent(findRect.getBottom() + offectY), text)) {
            return true
        }
    }
    else if (typeof (text2) == 'number') {
        offectY = text2
        if (ocrTextMoreInRegion(0, screen.heightToPercent(findRect.getTop() - offectY), 1, screen.heightToPercent(findRect.getBottom() + offectY), text)) {
            return true
        }
    }
    else if (typeof (text3) == 'number') {
        offectY = text3
        if (ocrTextMoreInRegion(0, screen.heightToPercent(findRect.getTop() - offectY), 1, screen.heightToPercent(findRect.getBottom() + offectY), text, text2)) {
            return true
        }
    } else {
        if (ocrTextMoreInRegion(0, screen.heightToPercent(findRect.getTop() - offectY), 1, screen.heightToPercent(findRect.getBottom() + offectY), text, text2, text3)) {
            return true
        }
    }

    // SystemPrint.log(typeof (text3))


    log('不再目标附近发现: ' + text)
    return false
}

function getDectByNearBy(findView, text, offectY) {
    if (!findView) {
        return []
    }
    if (offectY == undefined) offectY = 250

    var findRect = findView.getRealRect()

    var img = screen.screenShotFull();
    if (img.isNull()) {
        // requestScreen权限()
        print.err('截图失败!!!')
        return []
    }
    // var cutimg = img.cutImagePercent(percentX1, percentY1, percentX2, percentY2)
    // var ocrRes = cutimg.MLKitOcr('zhs');
    var ocrRes = img.MLKitOcrRect('zhs', [screen.widthToPercent(20), screen.heightToPercent(findRect.getTop() - offectY), 1, screen.heightToPercent(findRect.getBottom() + offectY)]);
    // printocrinfo(ocrRes)
    img.recycle()

    dect = ocrRes.findIncludeText(text);
    if (!dect && CONVERT_ARRAY.some(str => text.includes(str))) {
        dect = replaceFind(ocrRes, text, 0)
    }
    // SystemPrint.log(ocrRes.getAllString()
    if (dect) {
        // var rect = dect.getRealRect()
        // rect.set(rect.getLeft() + screen.percentToWidth(percentX1), rect.getTop() + screen.percentToHeight(percentY1), rect.getRight() + screen.percentToWidth(percentX1), rect.getBottom() + screen.percentToHeight(percentY1))
        printocrinfo(dect)
    }
    return dect
}


function getMaxNumByNearByOffecXY(findView, offectLeftX, offectTopY, offectRightX, offectBottomY) {
    if (!findView) {
        return []
    }
    if (offectLeftX == undefined) offectLeftX = 0
    if (offectTopY == undefined) offectTopY = 200
    if (offectRightX == undefined) offectRightX = 0
    if (offectBottomY == undefined) offectBottomY = 200

    var findRect = findView.getRealRect()

    var img = screen.screenShotFull();
    if (img.isNull()) {
        // requestScreen权限()
        print.err('截图失败!!!')
        return []
    }
    // var cutimg = img.cutImagePercent(percentX1, percentY1, percentX2, percentY2)
    // var ocrRes = cutimg.MLKitOcr('zhs');
    // SystemPrint.log(findRect.getLeft() - offectLeftX)
    // SystemPrint.log(offectLeftX)
    var ocrRes = img.MLKitOcrRect('zhs', [screen.widthToPercent(findRect.getLeft() - offectLeftX), screen.heightToPercent(findRect.getTop() - offectTopY), screen.widthToPercent(findRect.getRight() + offectRightX), screen.heightToPercent(findRect.getBottom() + offectBottomY)]);
    // printocrinfo(ocrRes)
    img.recycle()


    SystemPrint.log(ocrRes.getAllString())
    var numbers = ocrRes.getAllString().match(/(\d+)/g) || [];
    // SystemPrint.log(numbers.toString())

    // return numbers
    // 转换为数字数组并找到最大值
    var maxNum = Math.max.apply(null, numbers.map(function (n) { return parseInt(n, 10); }));
    SystemPrint.log('获取到最大值: ' + maxNum)
    return maxNum
}

function getNumByNearBy(findView, offectX, offectY) {
    if (!findView) {
        return []
    }
    if (offectX == undefined) offectX = 100
    if (offectY == undefined) offectY = 250

    var findRect = findView.getRealRect()

    var img = screen.screenShotFull();
    if (img.isNull()) {
        // requestScreen权限()
        print.err('截图失败!!!')
        return []
    }
    // var cutimg = img.cutImagePercent(percentX1, percentY1, percentX2, percentY2)
    // var ocrRes = cutimg.MLKitOcr('zhs');
    var ocrRes = img.MLKitOcrRect('zhs', [screen.widthToPercent(findRect.getLeft() - offectX), screen.heightToPercent(findRect.getTop() - offectY), screen.widthToPercent(findRect.getRight() + offectX), screen.heightToPercent(findRect.getBottom() + offectY)]);
    // printocrinfo(ocrRes)
    img.recycle()


    SystemPrint.log(ocrRes.getAllString())
    var numbers = ocrRes.getAllString().match(/(\d+)/g) || [];
    // SystemPrint.log(numbers.toString())

    return numbers
    // 转换为数字数组并找到最大值
    //  Math.max.apply(null, numbers.map(function (n) { return parseInt(n, 10); }));
}

function getMinNumByNearBy(findView, offectX, offectY) {
    var minNum = Math.min.apply(null, getNumByNearBy(findView, offectX, offectY).map(function (n) { return parseInt(n, 10); }));
    SystemPrint.log('获取到最小值: ' + minNum)
    return minNum
}

function getMaxNumByNearBy(findView, offectX, offectY) {
    var minNum = Math.max.apply(null, getNumByNearBy(findView, offectX, offectY).map(function (n) { return parseInt(n, 10); }));
    SystemPrint.log('获取到最大值: ' + minNum)
    return minNum
}


//↑----------------------------------

// //首先全屏截图

// var img=screen.screenShotFull();

// //然后再按照百分比坐标裁剪
// var cutimg=img.cutImagePercent(0.566,0.4297,0.6701,0.4883)

// //打印输出图片
// printocrinfo(cutimg);

// //不用的时候回收内存
// img.recycle();

